import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/product_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/product_storage_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
import 'package:get/get.dart';

class ProductDetailsController extends GetxController {
  /// 产品id
  String id = Get.arguments as String;

  /// 是否是展开状态
  bool isExpanded = false;

  /// 产品信息
  ProductEntity productEntity = ProductEntity();

  /// 产品信息
  List<ProductStorageEntity> productStorageList = [];

  /// 产品储存量
  List<ProductStorageEntity> productList = [];

  /// 点击展开或者收起
  void onExpanded() {
    isExpanded = !isExpanded;
    update();
  }

  /// 进入出库记录页面
  void toOutboundRecordsPage() {
    Get.toNamed(AppRoutes.outboundRecords, arguments: {'productId': id});
  }

  @override
  void onInit() {
    super.onInit();
    // 获取产品信息
    _getProductInformation();
    // 获取存储量
    _getProductStorageList();
  }

  /// 获取产品信息
  void _getProductInformation() async {
    final res = await HttpUtil().generateGetRequest<ProductEntity>(
      queryParameters: {'id': id},
      Interface.getProductRecord,
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        productEntity = result.data!;
      }
    });
    update();
  }

  /// 获取产品存储量列表
  void _getProductStorageList() async {
    final res = await HttpUtil().generateGetRequest<List<ProductStorageEntity>>(
      queryParameters: {'productId': id},
      Interface.getProductStorageList,
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        productList = result.data!;
      }
    });
    update();
  }
}
